import regExp from '@/libs/regExp'
function flitterVla (val, type) {
  let newVla = val
  switch (type) {
    // 大于等于0的整数
    case 'integerNumber':
      if (val !== '') {
        if (newVla.replace(regExp.nan, '') !== '') {
          newVla = Number.parseFloat(newVla.replace(regExp.nan, ''))
        } else {
          newVla = ''
        }
      }
      break
    case 'toUpperCase':
      newVla = newVla.toUpperCase()
      break
    default:

      break
  }
  return newVla
}
let _timer
export default {
  componentUpdated: (el, { value, modifiers }, vnode) => {
    // console.log(el.getElementsByTagName('input')[0].value)
    let options = Object.assign(
      {
        reg: /\s+/g, // 正则
        debounce: false, // 防抖
        delay: 100, // 防抖时间
        type: null // 类型
      },
      Object.prototype.toString.call(value) === '[object Object]' ? value : {}
    )
    const input = el.querySelector('input')
    let val = el.querySelector('input').value
    if (val === '' || input !== document.activeElement) { // 如果值为空或者不是当前元素不触发
      return null
    }
    if (_timer) window.clearTimeout(_timer)
    let event = document.createEvent('Event')
    event.initEvent(modifiers.lazy ? 'change' : 'input', true, true)
    if (options.debounce) {
      _timer = window.setTimeout(() => {
        try {
          // console.log(options.reg, '防抖')

          input.value = options.type ? flitterVla(val.replace(options.reg, ''), options.type) : val.replace(options.reg, '')
          input.dispatchEvent(event)
        } catch (e) {}
      }, options.delay)
    } else {
      try {
        input.value = options.type ? flitterVla(val.replace(options.reg, ''), options.type) : val.replace(options.reg, '')
        input.dispatchEvent(event)
      } catch (e) {}
    }
  }
}
